/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telas;

import classes.Pergunta;
import classes.Xml;
import java.util.Vector;
import javax.swing.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Bel
 */
public class TelaCadastrarPergunta extends javax.swing.JFrame {

    /**
     * Creates new form TelaCadastrarPergunta
     */
    private Vector<String> materias = new Vector<>();
    Xml xmlMateria = new Xml("materia.xml");

    public TelaCadastrarPergunta() {
        initComponents();
        this.setLocationRelativeTo(null);

        Document doc = xmlMateria.carregaXml();

        Element elem = doc.getDocumentElement();
        NodeList nl = elem.getElementsByTagName("materia");
        for (int i = 0; i < nl.getLength(); i++) {
            Element tag = (Element) nl.item(i);
            materias.add(tag.getAttribute("descricao"));
        }
        ComboBoxModel boxModel = new DefaultComboBoxModel(materias);
        cmbMateria.setModel(boxModel);
    }

    TelaCadastrarPergunta(Pergunta pergunta) {
        initComponents();
        this.setLocationRelativeTo(null);

        Document doc = xmlMateria.carregaXml();

        Element elem = doc.getDocumentElement();
        NodeList nl = elem.getElementsByTagName("materia");
        for (int i = 0; i < nl.getLength(); i++) {
            Element tag = (Element) nl.item(i);
            materias.add(tag.getAttribute("descricao"));
        }
        ComboBoxModel boxModel = new DefaultComboBoxModel(materias);
        cmbMateria.setModel(boxModel);
        
        txtDescricao.setText(pergunta.getDescricao());
        txtOpcao1.setText(pergunta.getRespostas()[0]);
        txtOpcao2.setText(pergunta.getRespostas()[1]);
        txtOpcao3.setText(pergunta.getRespostas()[2]);
        txtOpcao4.setText(pergunta.getRespostas()[3]);
    }

    private boolean estaVazio(JTextField campo) {
        return campo.getText().trim().equals("");
    }

    private boolean estaVazio(JTextArea campo) {
        return campo.getText().trim().equals("");
    }

    private void mostraMensagem(String mensagem) {
        JOptionPane.showMessageDialog(this, mensagem, "Nova pergunta", JOptionPane.INFORMATION_MESSAGE);
    }

    private boolean validaCampos() {
        boolean passou = true;
        if (estaVazio(txtDescricao)) {
            mostraMensagem("Preencha a descrição da pergunta");
            passou = false;
        }

        if (estaVazio(txtOpcao1) || estaVazio(txtOpcao2)
                || estaVazio(txtOpcao3) || estaVazio(txtOpcao4)) {
            mostraMensagem("Preencha as quatro opções de resposta");
            passou = false;
        }
        return passou;
    }

    private int getRespostaCorreta() {
        int r;
        if (rdbOpcao1.isSelected()) {
            r = 1;
        } else if (rdbOpcao2.isSelected()) {
            r = 2;
        } else if (rdbOpcao3.isSelected()) {
            r = 3;
        } else {
            r = 4;
        }
        return r;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lblCadastrarNovaPergunta = new javax.swing.JLabel();
        lblDescricao = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescricao = new javax.swing.JTextArea();
        lblOpcao1 = new javax.swing.JLabel();
        lblOpcao2 = new javax.swing.JLabel();
        lblOpcao3 = new javax.swing.JLabel();
        lblOpcao4 = new javax.swing.JLabel();
        txtOpcao1 = new javax.swing.JTextField();
        txtOpcao2 = new javax.swing.JTextField();
        txtOpcao3 = new javax.swing.JTextField();
        txtOpcao4 = new javax.swing.JTextField();
        rdbOpcao1 = new javax.swing.JRadioButton();
        rdbOpcao2 = new javax.swing.JRadioButton();
        rdbOpcao3 = new javax.swing.JRadioButton();
        rdbOpcao4 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        lblNivel = new javax.swing.JLabel();
        cmbNivel = new javax.swing.JComboBox();
        lblMateria = new javax.swing.JLabel();
        cmbMateria = new javax.swing.JComboBox();
        btnSalvarPergunta = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Nova Pergunta");
        setResizable(false);

        lblCadastrarNovaPergunta.setText("Cadastrar Nova Pergunta");

        lblDescricao.setLabelFor(txtDescricao);
        lblDescricao.setText("Descrição");

        txtDescricao.setColumns(20);
        txtDescricao.setRows(5);
        jScrollPane1.setViewportView(txtDescricao);

        lblOpcao1.setText("Opção 1");

        lblOpcao2.setText("Opção 2");

        lblOpcao3.setText("Opção 3");

        lblOpcao4.setText("Opção 4");

        txtOpcao1.setColumns(40);

        txtOpcao2.setColumns(40);

        txtOpcao3.setColumns(40);

        txtOpcao4.setColumns(40);

        buttonGroup1.add(rdbOpcao1);
        rdbOpcao1.setSelected(true);

        buttonGroup1.add(rdbOpcao2);

        buttonGroup1.add(rdbOpcao3);

        buttonGroup1.add(rdbOpcao4);

        jLabel1.setText("Digite as opções de resposta e marque a correta");

        lblNivel.setLabelFor(cmbNivel);
        lblNivel.setText("Nível");

        cmbNivel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fácil", "Médio", "Difícil" }));

        lblMateria.setLabelFor(cmbMateria);
        lblMateria.setText("Matéria");

        btnSalvarPergunta.setText("Salvar Pergunta");
        btnSalvarPergunta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadastrarPergunta(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblOpcao4)
                                    .addComponent(lblOpcao3)
                                    .addComponent(lblOpcao2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtOpcao2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtOpcao4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtOpcao3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(lblOpcao1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(txtOpcao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblDescricao)
                                        .addGap(18, 18, 18))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(lblNivel)
                                        .addGap(41, 41, 41)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cmbNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(26, 26, 26)
                                        .addComponent(lblMateria)
                                        .addGap(22, 22, 22)
                                        .addComponent(cmbMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jScrollPane1))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rdbOpcao4)
                            .addComponent(rdbOpcao3)
                            .addComponent(rdbOpcao2)
                            .addComponent(rdbOpcao1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(129, 129, 129)
                                .addComponent(lblCadastrarNovaPergunta))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(149, 149, 149)
                                .addComponent(btnSalvarPergunta)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCadastrarNovaPergunta)
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNivel)
                    .addComponent(cmbNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblMateria)
                    .addComponent(cmbMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(lblDescricao)))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtOpcao1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblOpcao1)
                    .addComponent(rdbOpcao1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtOpcao2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblOpcao2)
                    .addComponent(rdbOpcao2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtOpcao3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblOpcao3))
                    .addComponent(rdbOpcao3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblOpcao4)
                        .addComponent(txtOpcao4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(rdbOpcao4, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addComponent(btnSalvarPergunta)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cadastrarPergunta(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastrarPergunta
        int respostaCorreta = getRespostaCorreta();
        Pergunta pergunta = new Pergunta(txtDescricao.getText(), respostaCorreta,
                cmbNivel.getSelectedItem().toString(), cmbMateria.getSelectedItem().toString());
        String[] respostas = {txtOpcao1.getText(), txtOpcao2.getText(),
            txtOpcao3.getText(), txtOpcao4.getText()};
        pergunta.setRespostas(respostas);
        if (validaCampos()) {
            pergunta.cadastrarPergunta();
            mostraMensagem("Pergunta cadastrada com sucesso!");
        }
        

    }//GEN-LAST:event_cadastrarPergunta

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCadastrarPergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCadastrarPergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCadastrarPergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCadastrarPergunta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TelaCadastrarPergunta().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvarPergunta;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cmbMateria;
    private javax.swing.JComboBox cmbNivel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCadastrarNovaPergunta;
    private javax.swing.JLabel lblDescricao;
    private javax.swing.JLabel lblMateria;
    private javax.swing.JLabel lblNivel;
    private javax.swing.JLabel lblOpcao1;
    private javax.swing.JLabel lblOpcao2;
    private javax.swing.JLabel lblOpcao3;
    private javax.swing.JLabel lblOpcao4;
    private javax.swing.JRadioButton rdbOpcao1;
    private javax.swing.JRadioButton rdbOpcao2;
    private javax.swing.JRadioButton rdbOpcao3;
    private javax.swing.JRadioButton rdbOpcao4;
    private javax.swing.JTextArea txtDescricao;
    private javax.swing.JTextField txtOpcao1;
    private javax.swing.JTextField txtOpcao2;
    private javax.swing.JTextField txtOpcao3;
    private javax.swing.JTextField txtOpcao4;
    // End of variables declaration//GEN-END:variables
}
